package com.dayaokeji.rhythmschoolstudent.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.dayaokeji.rhythmschoolstudent.R;


/**
 * Created by Gorden on 2018/1/29.
 *
 * 统计进度
 */

public class StatisticsProgressView extends FrameLayout {

    private View statisticsProgressView;
    private ProgressBar pbStatistics;
    private TextView tvStatisticsPercent;

    public StatisticsProgressView(Context context) {
        this(context,null);
    }

    public StatisticsProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public StatisticsProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        if (statisticsProgressView == null) {
            statisticsProgressView = LayoutInflater.from(getContext()).inflate(R.layout.view_statistics_progress, null, false);
        }

        pbStatistics = statisticsProgressView.findViewById(R.id.pb_statistics);
        tvStatisticsPercent = statisticsProgressView.findViewById(R.id.tv_statistics_percent);

        this.addView(statisticsProgressView);
    }


    private Drawable progressDrawableByPercent(int percent) {
        Drawable drawable = getContext().getResources().getDrawable(R.drawable.progress_bar_style_1);
        if (percent <= 20) {
            drawable = getContext().getResources().getDrawable(R.drawable.progress_bar_percent_20);
        }else if (percent > 20 && percent <= 60) {
            drawable = getContext().getResources().getDrawable(R.drawable.progress_bar_percent_60);
        }else if (percent > 60 && percent <= 80) {
            drawable = getContext().getResources().getDrawable(R.drawable.progress_bar_percent_80);
        }else if (percent > 80 && percent <= 100) {
            drawable = getContext().getResources().getDrawable(R.drawable.progress_bar_percent_100);
        }
        return drawable;
    }

    private int progressTextColorByPercent(int percent) {
        int color = 0;
        if (percent <= 20) {
            color = getContext().getResources().getColor(R.color.statistics_percent_20);
        }else if (percent > 20 && percent <= 60) {
            color = getContext().getResources().getColor(R.color.statistics_percent_60);
        }else if (percent > 60 && percent <= 80) {
            color = getContext().getResources().getColor(R.color.statistics_percent_80);
        }else if (percent > 80 && percent <= 100) {
            color = getContext().getResources().getColor(R.color.statistics_percent_100);
        }
        return color;
    }


    public void setStatisticsProgress(float percent) {
        pbStatistics.setProgressDrawable(progressDrawableByPercent((int) percent));
        pbStatistics.setProgress((int) percent);
        tvStatisticsPercent.setText("%"+(int)percent);
        tvStatisticsPercent.setTextColor(progressTextColorByPercent((int) percent));
    }
}
